var app = getApp();

Page({
    data: {
        array: [
            "京",
            "浙",
            "津",
            "皖",
            "沪",
            "闽",
            "渝",
            "赣",
            "港",
            "鲁",
            "澳",
            "豫",
            "蒙",
            "鄂",
            "新",
            "湘",
            "宁",
            "粤",
            "藏",
            "琼",
            "桂",
            "川",
            "蜀",
            "冀",
            "贵",
            "黔",
            "晋",
            "云",
            "滇",
            "辽",
            "陕",
            "吉",
            "甘",
            "陇",
            "黑",
            "青",
            "苏",
            "台"
        ],
        index: 2, //省份简写数组索引值
        proviceName: "津",
        date: "2016-09-01", //添加日期
        time: "12:01", //添加时间
        isAgree: true, //是否同意协议
        warnMsg: "错误提示", //错误提示信息
        CarNum: 0, //车牌号码
        pinpai: [],
        pinpaiIndex: 1000,
        pinpaiIds: [],
        pinpaiId: 0, //品牌id
        pinpaiName: "", //品牌名字
        chexi: [],
        chexiIndex: 1000,
        chexiIds: [],
        chexiId: 0, //车系id
        chexiName: "", //车系名字
        chexing: [],
        chexingIndex: 1000,
        chexingIds: [],
        chexingId: 0, //车型id
        chexingName: "", //车型名字
        licenseplate: "" //车牌号
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //三级联动
        var url = "obd/car/selectCarop";
        var data = 0;
        var callback = res => {
            var pinpai = [];
            var pinpais = res.data;
            var pinpaiIds = [];
            var logos = [];
            for (var i = 0; i < pinpais.length; i++) {
                logos.push(pinpais[i].LOGO_PATH);
                pinpai.push(pinpais[i].NAME);
                pinpaiIds.push(pinpais[i].ID);
            }
            this.setData({
                logos: logos,
                pinpai: pinpai,
                pinpaiIds: pinpaiIds
            });
        };
        app.getRequest(url, data, callback);
        var that = this;
        that.setData({
            c_id: options.c_id //汽车id
        });
        //获取车辆信息
        var carUrl = "api/car/carInfo";
        var cardata = {
            c_id: options.c_id
        };
        this.setData({
            c_id: options.c_id
        });
        var carcallback = res => {
            var proviceName = res.data.licenseplate.substring(0, 1); //省份的首字
            var licenseplate = res.data.licenseplate.slice(1); //车牌号
            that.setData({
                proviceName: proviceName, //省份
                licenseplate: licenseplate, //车牌号
                pinpaiName: res.data.b_name, //品牌name
                pinpaiId: res.data.b_id, //品牌id
                chexingName: res.data.m_name, //车型name
                chexingId: res.data.m_id, //车型id
                chexiName: res.data.t_name, //车系name
                chexiId: res.data.t_id, //车系id
                logo: res.data.logo //车logo
            });
        };
        app.getRequest(carUrl, cardata, carcallback);
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 判断是否登录
        app.getUserInfoFun();
        this.setData({
            b_id: wx.getStorageSync("b_id"),
            t_id: wx.getStorageSync("t_id"),
            m_id: wx.getStorageSync("m_id"),
            pinpaiName: wx.getStorageSync("pinpaiName"),
            chexiName: wx.getStorageSync("chexiName"),
            chexingName: wx.getStorageSync("chexingName"),
            CarNum: wx.getStorageSync("CarNum")
        });
    },
    bindPickerChange: function (e) {
        this.setData({
            index: e.detail.value
        });
    },

    //点击跳转到三级联动页面
    tolinkage: function () {
        wx.removeStorageSync('b_id');
        wx.removeStorageSync('t_id');
        wx.removeStorageSync('m_id');
        wx.removeStorageSync('pinpaiName');
        wx.removeStorageSync('chexiName');
        wx.removeStorageSync('chexingName');
        wx.navigateTo({
            url: "../linkage/linkage"
        });
    },

    bindPinpaiChange: function (e) {
        this.data.pinpaiName = this.data.pinpai[e.detail.value];
        this.setData({
            pinpaiIndex: e.detail.value,
            pinpaiId: this.data.pinpaiIds[e.detail.value],
            logo: this.data.logos[e.detail.value],
            pinpaiName: this.data.pinpai[e.detail.value]
        });
        wx.setStorageSync(
            "defaultCar_img",
            app.globalData.globalUrl + this.data.logo
        );
        var thisID = this.data.pinpaiIds[e.detail.value];
        thisID = thisID - 0;
        var that = this;
        var url = "obd/car/selectCarop";
        var data = {
            p_id: thisID
        };
        var callback = res => {
            var chexi = [];
            var chexis = res.data;
            var chexiIds = [];
            for (var i = 0; i < chexis.length; i++) {
                chexi.push(chexis[i].NAME);
                chexiIds.push(chexis[i].ID);
            }
            that.setData({
                chexi: chexi,
                chexiIds: chexiIds
            });
        };
        app.getRequest(url, data, callback);
    },
    bindChexiChange: function (e) {
        this.data.chexiName = this.data.chexi[e.detail.value];
        this.setData({
            chexiIndex: e.detail.value,
            chexiId: this.data.chexiIds[e.detail.value],
            chexiName: this.data.chexi[e.detail.value]
        });
        var thisID = this.data.chexiIds[e.detail.value];
        thisID = thisID - 0;
        var that = this;
        var url = "obd/car/selectCarop";
        var data = {
            p_id: thisID
        };
        var callback = res => {
            var chexing = [];
            var chexings = res.data;
            var chexingIds = [];
            for (var i = 0; i < chexings.length; i++) {
                chexing.push(chexings[i].NAME);
                chexingIds.push(chexings[i].ID);
            }
            that.setData({
                chexing: chexing,
                chexingIds: chexingIds
            });
        };
        app.getRequest(url, data, callback);
    },
    bindChexingChange: function (e) {
        this.setData({
            chexingIndex: e.detail.value,
            chexingId: this.data.chexingIds[e.detail.value],
            chexingName: this.data.chexing[e.detail.value]
        });
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        });
    },
    bindTimeChange: function (e) {
        this.setData({
            time: e.detail.value
        });
    },
    bindEngineInput: function (e) {
        this.setData({
            EngineNum: e.detail.value
        });
    },
    showTopTips: function () {
        //提交按钮点击事件
        // 车牌号码正则
        var express = /^[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
        var that = this;
        if (!express.test(this.data.licenseplate)) {
            this.setData({
                warnMsg: "车牌号码输入有误"
            });
            this.setData({
                showTopTips: true
            });
            setTimeout(function () {
                that.setData({
                    showTopTips: false
                });
            }, 2000);
            return;
        }
        //ajax提交
        var that = this;
        var u_id = wx.getStorageSync("u_id");
        var url = "api/car/carEdit";
        var data = {
            u_id: u_id,
            c_id: this.data.c_id,
            b_id: this.data.b_id,
            t_id: this.data.t_id,
            m_id: this.data.m_id,
            b_name: this.data.pinpaiName,
            t_name: this.data.chexiName,
            m_name: this.data.chexingName,
            licenseplate: this.data.proviceName + this.data.licenseplate,
            //device_name: this.data.device_name,
            logo: this.data.logo
        };
        app.getRequest(url, data, res => {
            if (res.code == 200) {
                wx.setStorageSync("cartype_id", this.data.pinpaiId);
                this.setData({
                    warnMsg: "编辑成功"
                });
                this.setData({
                    showTopTips: true
                });
                setTimeout(res => {
                    this.setData({
                        showTopTips: false
                    });
                }, 2000);
                setTimeout(function () {
                    wx.navigateTo({
                        url: "../myCar/myCar"
                    });
                }, 2000);
            } else {
                this.setData({
                    warnMsg: res.message
                });
                this.setData({
                    showTopTips: true
                });
                setTimeout( res=> {
                    this.setData({
                        showTopTips: false
                    });
                }, 2000);
            }

        });
    },
    bindCarInput: function (e) {
        this.setData({
            licenseplate: e.detail.value
        });
    },
    bindCountryChange: function (e) {
        this.setData({
            countryIndex: e.detail.value
        });
    },

    deleteThis: function () {
      app.getRequest("api/car/carDel", { c_id: this.data.c_id}, function(res){
        if(res.code == 200) {
          wx.navigateBack({})
          wx.showToast({
            title: '删除成功',
            icon: "none"
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    }
});